﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Core;
using dtSearch32Lib;
using DocumentSearchServiceModel;
using DocumentSearch32Lib;
using Portal.Systems.Model;
using Portal.Workflow.Bussiness;
using Portal.Cms.Utility;
using System.Data;
using System.Collections.ObjectModel;

namespace Portal.Cms.Render
{
    public class UpdateCmsDataRenderBll
    {
        static string GetPlugInJavascript(string SiteId)
        {
            string ScriptText =
                   "<script language=javascript>\r\n" +
                   "    function ProcessHotCategory(Id)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        checked=document.getElementById('chkHot'+Id).checked;\r\n" +
                   "        CheckedIds=document.getElementById('txtCheckedHotCategory').value;\r\n" +
                   "        CheckedIds = Portal.Cms.Render.UpdateCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,checked).value;\r\n" +
                   "        document.getElementById('txtCheckedHotCategory').value=CheckedIds;\r\n" +


                   "        if(checked)\r\n" +
                   "        {\r\n" +
                   "            document.getElementById('chk'+Id).checked=true;\r\n" +

                   "            CheckedIds=document.getElementById('txtCheckedCategory').value;\r\n" +
                   "            CheckedIds = Portal.Cms.Render.UpdateCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,true).value;\r\n" +
                   "            document.getElementById('txtCheckedCategory').value=CheckedIds;\r\n" +


                   "        }\r\n" +
                   "    }\r\n" +

                   "    function ProcessCheckCategory(Id)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        checked=document.getElementById('chk'+Id).checked;\r\n" +
                   "        CheckedIds=document.getElementById('txtCheckedCategory').value;\r\n" +
                   "        CheckedIds = Portal.Cms.Render.UpdateCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,checked).value;\r\n" +
                   "        document.getElementById('txtCheckedCategory').value=CheckedIds;\r\n" +
                   "        if(checked==false)\r\n" +
                   "        {\r\n" +
                   "            document.getElementById('chkHot'+Id).checked=false;\r\n" +
                   "            CheckedIds=document.getElementById('txtCheckedHotCategory').value;\r\n" +
                   "            CheckedIds = Portal.Cms.Render.UpdateCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,false).value;\r\n" +
                   "            document.getElementById('txtCheckedHotCategory').value=CheckedIds;\r\n" +
                   "        }\r\n" +
                   "    }\r\n" +
                   "</script>\r\n";
            return ScriptText;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideProcessCheckCategory(string SiteId, string CheckedIds, string Id, bool Checked)
        {
            if (Checked)
            {
                if (!string.IsNullOrEmpty(CheckedIds))
                {
                    CheckedIds = CheckedIds + ";";
                }
                CheckedIds = CheckedIds + Id;
            }
            else
            {
                CheckedIds = CheckedIds.Replace(Id, "");
            }
            CheckedIds = CheckedIds.Replace(";;", ";");
            if (CheckedIds.Length > 0)
            {
                if (CheckedIds[0].ToString().Equals(";"))
                {
                    CheckedIds = CheckedIds.Substring(1);
                }
            }
            return CheckedIds;
        }




        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                string CmsDataId = (string)WebEnvironments.Request(SiteId, "CmsDataId");
                string BackUrl = (string)WebEnvironments.Request(SiteId, "Back");
                if (string.IsNullOrEmpty(BackUrl))
                {
                    BackUrl = "InputData.aspx?gid=" + GroupDataId;
                }
                //kiem tra xem co trong quy trinh khong da
                string Html =
                   GetPlugInJavascript(SiteId) +
                   DrawUpdateForm(SiteId, GroupDataId, CmsDataId, BackUrl).HtmlContent;

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
        

        public static string GetSelect(string SiteId, string FileName, string ComboboxId, string FocusId)
        {
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string Xml = OSiteParam.PathRoot + "\\Admin\\XmlData\\News\\" + FileName;
                DataSet ds = new DataSet();
                ds.ReadXml(Xml);
                string SelectData =
                    " <select id=\"" + ComboboxId + "\" class=drplist>\r\n" +
                    "   <option value=\"\"></option>\r\n";
                for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
                {
                    string Id = (string)ds.Tables[0].Rows[iIndex]["Id"];
                    string Name = (string)ds.Tables[0].Rows[iIndex]["Name"];
                    if (FocusId.Equals(Id))
                    {
                        SelectData += "<option selected value=\"" + Id + "\">" + Name + "</option>\r\n";
                    }
                    else
                    {
                        SelectData += "<option value=\"" + Id + "\">" + Name + "</option>\r\n";
                    }
                }
                SelectData += " </select>\r\n";
                return SelectData;
            }
            catch
            {
                return "";
            }
        }


        static bool Check(string[] Items,string Id)
        {
            for (int iIndex = 0; iIndex < Items.Length; iIndex++)
            {
                if (Items[iIndex].Trim().Equals(Id.Trim()))
                {
                    return true;
                }
            }
            return false;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildCategory(
            string SiteId, 
            string DataLanguage,
            string Categories,
            string CategoryIds, 
            string HotCategoryIds)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string[] ItemCategoryIds = CategoryIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                string[] ItemHotCategoryIds = HotCategoryIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                
                //string XmlFile = OSiteParam.PathRoot + "\\admin\\xmldata\\news\\Category\\" + DataLanguage + "\\Category.xml";
                //DataSet ds = new DataSet();
                //ds.ReadXml(XmlFile);
                //DataView dv = new DataView(ds.Tables[0], "ParentId='0'", "", DataViewRowState.CurrentRows);

                GroupDataCategoryCls[] GroupDataCategories = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataCategory(SiteId, DataLanguage);
              

                string Html =
                    "<div class=CssListField>\r\n" +
                    "<ul id=\"TreeView\" class=\"TreeView\">\r\n";
                for (int iIndex = 0; iIndex < GroupDataCategories.Length; iIndex++)
                {
                    string Id = GroupDataCategories[iIndex].GroupDataCategoryId;
                    string Name = GroupDataCategories[iIndex].Name;

                    //DataView dvSub = new DataView(ds.Tables[0], "ParentId='" + Id + "'", "", DataViewRowState.CurrentRows);
                    if (GroupDataCategories[iIndex].SubGroupDataCategories.Count>0)
                    {
                        bool Found = false;
                        AjaxOut OAjaxOutReadingSubField = ServerSideReadingSubField(SiteId, GroupDataCategories[iIndex].SubGroupDataCategories, DataLanguage, Categories, Id, ItemCategoryIds, ItemHotCategoryIds);
                        Found = OAjaxOutReadingSubField.RetBoolean;
                        if (Found)
                        {
                            Html += " <li id=\"" + Id + "\" class=\"Expanded\">\r\n" +
                                        "<a href=\"javascript:ProcessExpandCollapse('" + Id + "');\"><span id=\"spanMainFieldName" + Id + "\">" + Name + "</span></a>\r\n" +
                                            "<input type=hidden value=\"0\" id=\"txtReadSubField" + Id + "\"><ul id=\"ulSubField" + Id + "\">\r\n" +
                                               OAjaxOutReadingSubField.HtmlContent+
                                            "</ul>\r\n" +
                                        "</li>\r\n";
                        }
                    }
                    else
                    {
                        if (Categories.IndexOf("$" + Id + "$") != -1)
                        {
                            string CheckCategory = "";
                            if (Check(ItemCategoryIds, Id))
                            {
                                CheckCategory = " CHECKED ";
                            }
                            string HotCheckCategory = "";
                            if (Check(ItemHotCategoryIds, Id))
                            {
                                HotCheckCategory = " CHECKED ";
                            }
                            Html += " <li id=\"" + Id + "\" class=\"Expanded\">\r\n" +
                                    "<input " + CheckCategory + " type=checkbox class=checkbox onchange=\"javascript:ProcessCheckCategory('" + Id + "','')\" id=\"chk" + Id + "\" style=\"margin-right:4px;\"><span id=\"spanMainFieldName" + Id + "\">" + Name + "</span><input id=\"chkHot" + Id + "\" style=\"margin-left:5px;\" type=checkbox class=checkbox " + HotCheckCategory + " onchange=\"javascript:ProcessHotCategory('" + Id + "');\">Nóng chuyên mục\r\n" +
                                            "<input type=hidden value=\"0\" id=\"txtReadSubField" + Id + "\"><ul id=\"ulSubField" + Id + "\">\r\n" +

                                            "</ul>\r\n" +
                                        "</li>\r\n";
                        }
                    }
                }
                Html += "</ul>\r\n" +
                      "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
                
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingSubField(
            string SiteId, 
            Collection<GroupDataCategoryCls> SubGroupDataCategories,
            string DataLanguage,
            string Categories,
            string CategoryId, 
            string[] ItemCategoryIds, 
            string[] ItemHotCategoryIds)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                bool found = false;
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                //string XmlFile = OSiteParam.PathRoot + "\\admin\\xmldata\\news\\Category\\" + DataLanguage + "\\Category.xml";
                //DataSet ds = new DataSet();
                //ds.ReadXml(XmlFile);
                //DataView dv = new DataView(ds.Tables[0], "ParentId='" + CategoryId + "'", "", DataViewRowState.CurrentRows);

                string Html = "";
                for (int jIndex = 0; jIndex < SubGroupDataCategories.Count; jIndex++)
                {
                    string Id = SubGroupDataCategories[jIndex].GroupDataCategoryId;//["Id"].ToString();
                    string Name = SubGroupDataCategories[jIndex].Name;
                    
                    if (Categories.IndexOf("$" + Id + "$") != -1)
                    {
                        found = true;
                        string CheckCategory = "";
                        if (Check(ItemCategoryIds, Id))
                        {
                            CheckCategory = " CHECKED ";
                        }
                        string HotCheckCategory = "";
                        if (Check(ItemHotCategoryIds, Id))
                        {
                            HotCheckCategory = " CHECKED ";
                        }

                        Html += " <li class=liSubTreeViewItem>\r\n" +
                                "       <input " + CheckCategory + " type=checkbox class=checkbox onchange=\"javascript:ProcessCheckCategory('" + Id + "','')\" id=\"chk" + Id + "\" style=\"margin-right:4px;\"><span id=\"spanMainFieldName" + Id + "\">" + Name + "</span><input " + HotCheckCategory + " id=\"chkHot" + Id + "\" style=\"margin-left:5px;\" type=checkbox class=checkbox onchange=\"javascript:ProcessHotCategory('" + Id + "');\">Nóng chuyên mục\r\n" +
                                " </li>\r\n";
                    }
                }
                OAjaxOut.RetNumber = SubGroupDataCategories.Count;
                OAjaxOut.HtmlContent = Html;
                OAjaxOut.RetBoolean = found;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }




        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateForm(string SiteId, string GroupDataId, string CmsDataId, string BackUrl)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, CmsDataId);
                if (OGroupData == null) throw new Exception("Tham số truy cập không hợp lệ. Nhóm tin không tìm thấy!");
                if (OCmsData == null) throw new Exception("Tham số truy cập không hợp lệ. Bài viết không tìm thấy hoặc đã được xóa!");

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                
                string StyleDisp = " style=\"display:none;\"";
                if (OGroupData.ShowBody)
                {
                    StyleDisp = "";
                }

                string BackScript = "javascript:window.open('InputData.aspx?gid=" + OGroupData.GroupDataId + "','_self');";
                if (!string.IsNullOrEmpty(BackUrl))
                {
                    BackScript = "javascript:window.open('"+BackUrl+"','_self');";
                }
              
                string Html =
                     "<script language=javascript>\r\n " +

                    "  function ProcessExpandCollapse(Id)\r\n" +
                    "  {\r\n" +
                    "     if(document.getElementById(Id).className=='Collapsed')\r\n" +
                    "     {\r\n" +
                    "         document.getElementById(Id).className='Expanded';\r\n" +
                    "     }\r\n" +
                    "     else\r\n" +
                    "     {\r\n" +
                    "         document.getElementById(Id).className='Collapsed';\r\n" +
                    "     }\r\n" +
                    "  }\r\n" +

                     " function CallSaveCmsData(CloseWindow)\r\n" +
                     " {\r\n" +
                     "      document.getElementById('divCallSaveAddDataProcessing').innerHTML='Tiến hành cập nhật và đánh index tìm kiếm...';\r\n" +
                     "      setTimeout('RealCallSaveCmsData('+CloseWindow+')',10);\r\n" +
                     " }\r\n" +

                     "  function RealCallSaveCmsData(CloseWindow)\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      GroupDataId='" + GroupDataId + "';\r\n" +
                     "      DataId=document.getElementById('txtDataId').value;\r\n" +
                     "      Alias = document.getElementById('txtAlias').value;\r\n" +
                     "          Subject = Alias;\r\n" +
                     // "      if(document.getElementById('txtSubject')!=null)\r\n" +
                     //"      {\r\n" +
                     //"          Subject = document.getElementById('txtSubject').value;\r\n" +
                     //"      }\r\n" +
                     "          OtherSubject = '';\r\n" +
                       "      if(document.getElementById('txtOtherSubject')!=null)\r\n" +
                     "      {\r\n" +
                     "          OtherSubject = document.getElementById('txtOtherSubject').value;\r\n" +
                      "      }\r\n" +
                      "      Description = '';\r\n" +
                      "      if(document.getElementById('txtDescription')!=null)\r\n" +
                     "      {\r\n" +
                     "      Description = document.getElementById('txtDescription').value;\r\n" +
                     "      }\r\n" +
                     //"      if(FCKeditorAPI!=null){\r\n" +
                     //"          oEditor = FCKeditorAPI.GetInstance('FCKeditorSapo');\r\n" +
                     //"          Description= oEditor.GetXHTML( true ); \r\n" +
                     //"      }\r\n" +

                     "      Body = '';\r\n" +
                     "      if(FCKeditorAPI!=null){\r\n" +
                     "          oEditor = FCKeditorAPI.GetInstance('FCKeditorBody');\r\n" +
                     "          Body= oEditor.GetXHTML( true ); \r\n" +
                     "      }\r\n" +

                       "      Source = '';\r\n" +

                       "      if(document.getElementById('txtSource')!=null)\r\n" +
                     "      {\r\n" +
                     "      Source = document.getElementById('txtSource').value;\r\n" +
                     "      }\r\n" +

                     "      Url = document.getElementById('txtUrl').value;\r\n" +
                     "      Target ='';\r\n" +
                       "      if(document.getElementById('txtTarget')!=null)\r\n" +
                     "      {\r\n" +
                     "      Target = document.getElementById('txtTarget').value;\r\n" +
                       "      }\r\n" +
                     "      SortIndex=document.getElementById('txtSortIndex').value;\r\n" +
                     "      DocType='';\r\n" +
                     "      if(document.getElementById('drpSelectDocType')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocType=document.getElementById('drpSelectDocType').value;\r\n" +
                     "      }\r\n" +

                     "      DocField='';\r\n" +
                     "      if(document.getElementById('drpSelectDocField')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocField=document.getElementById('drpSelectDocField').value;\r\n" +
                     "      }\r\n" +

                     "      DocReleaseFrom='';\r\n" +
                     "      if(document.getElementById('drpSelectDocReleaseFrom')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocReleaseFrom=document.getElementById('drpSelectDocReleaseFrom').value;\r\n" +
                     "      }\r\n" +
                     "      DocSigner='';\r\n" +
                     "      if(document.getElementById('drpSelectDocSigner')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocSigner=document.getElementById('drpSelectDocSigner').value;\r\n" +
                     "      }\r\n" +

                     "      DocStatus='';\r\n" +
                     "      if(document.getElementById('drpSelectDocStatus')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocStatus=document.getElementById('drpSelectDocStatus').value;\r\n" +
                     "      }\r\n" +

                     "      ReleaseDate='';\r\n" +
                     "      if(document.getElementById('txtReleaseDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          ReleaseDate=document.getElementById('txtReleaseDate').value;\r\n" +
                     "      }\r\n" +

                     "      EffectedDate='';\r\n" +
                     "      if(document.getElementById('txtEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EffectedDate=document.getElementById('txtEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      EndEffectedDate='';\r\n" +
                     "      if(document.getElementById('txtEndEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EndEffectedDate=document.getElementById('txtEndEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      SignDate='';\r\n" +
                     "      if(document.getElementById('txtSignDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          SignDate=document.getElementById('txtSignDate').value;\r\n" +
                     "      }\r\n" +

                     "      ReplaceDoc='';\r\n" +
                     "      if(document.getElementById('txtReplaceDoc')!=null)\r\n" +
                     "      {\r\n" +
                     "          ReplaceDoc=document.getElementById('txtReplaceDoc').value;\r\n" +
                     "      }\r\n" +

                     "      ModifyDoc='';\r\n" +
                     "      if(document.getElementById('txtModifyDoc')!=null)\r\n" +
                     "      {\r\n" +
                     "          ModifyDoc=document.getElementById('txtModifyDoc').value;\r\n" +
                     "      }\r\n" +

                     "      VoteType='';\r\n" +
                     "      if(document.getElementById('drpSelectVoteType')!=null)\r\n" +
                     "      {\r\n" +
                     "          VoteType=document.getElementById('drpSelectVoteType').value;\r\n" +
                     "      }\r\n" +
                     "      IsNew = '0';\r\n" +
                     //"      IsNew = document.getElementById('drpSelectIsNew').value;\r\n" +
                     "      Searchable=0;\r\n" +
                     "      if(document.getElementById('drpSelectSearchable')!=null)\r\n" +
                     "      {\r\n" +
                     "          Searchable=document.getElementById('drpSelectSearchable').value;\r\n" +
                     "      }\r\n" +
                     //     Dành cho báo
                     "      CategoryIds='';\r\n"+
                     "      if(document.getElementById('txtCheckedCategory')!=null)\r\n" +
                     "      {\r\n" +
                     "          CategoryIds=document.getElementById('txtCheckedCategory').value;\r\n" +
                     "      }\r\n" +
                     "      HotCategoryIds='';\r\n" +
                     "      if(document.getElementById('txtCheckedHotCategory')!=null)\r\n" +
                     "      {\r\n" +
                     "          HotCategoryIds=document.getElementById('txtCheckedHotCategory').value;\r\n" +
                     "      }\r\n" +
                     "      NewsTypeId='';\r\n" +
                     "      if(document.getElementById('drpSelectNewsType')!=null)\r\n" +
                     "      {\r\n" +
                     "          NewsTypeId=document.getElementById('drpSelectNewsType').value;\r\n" +
                     "      }\r\n" +
                     "      ImageLevelId='';\r\n" +
                     "      if(document.getElementById('drpSelectImageLevel')!=null)\r\n" +
                     "      {\r\n" +
                     "          ImageLevelId=document.getElementById('drpSelectImageLevel').value;\r\n" +
                     "      }\r\n" +
                     "      EventId='';\r\n" +
                     "      if(document.getElementById('drpSelectEvent')!=null)\r\n" +
                     "      {\r\n" +
                     "          EventId=document.getElementById('drpSelectEvent').value;\r\n" +
                     "      }\r\n" +
                     "      ExpImageCount='';\r\n" +
                     "      if(document.getElementById('txtImageCount')!=null)\r\n" +
                     "      {\r\n" +
                     "          ExpImageCount=document.getElementById('txtImageCount').value;\r\n" +
                     "      }\r\n" +
                     "      ExpImageLevelId='';\r\n" +
                     "      if(document.getElementById('drpSelectExploitation')!=null)\r\n" +
                     "      {\r\n" +
                     "          ExpImageLevelId=document.getElementById('drpSelectExploitation').value;\r\n" +
                     "      }\r\n" +
                     "      NewsLevelId='';\r\n" +
                     "      if(document.getElementById('drpSelectNewsLevel')!=null)\r\n" +
                     "      {\r\n" +
                     "          NewsLevelId=document.getElementById('drpSelectNewsLevel').value;\r\n" +
                     "      }\r\n" +
                     "      IsFocus='0';\r\n" +
                     "      if(document.getElementById('chkFocus')!=null)\r\n" +
                     "      {\r\n" +
                     "          if(document.getElementById('chkFocus').checked==true)\r\n"+
                     "          {\r\n"+
                     "              IsFocus='1';\r\n"+
                     "          }\r\n"+
                     "      }\r\n" +
                     "      IsShowAuthor='0';\r\n" +
                     "      if(document.getElementById('chkShowAuthor')!=null)\r\n" +
                     "      {\r\n" +
                     "          if(document.getElementById('chkShowAuthor').checked==true)\r\n" +
                     "          {\r\n" +
                     "              IsShowAuthor='1';\r\n" +
                     "          }\r\n" +
                     "      }\r\n" +
                     "      IsAdv='0';\r\n" +
                     "      if(document.getElementById('chkAdv')!=null)\r\n" +
                     "      {\r\n" +
                     "          if(document.getElementById('chkAdv').checked==true)\r\n" +
                     "          {\r\n" +
                     "              IsAdv='1';\r\n" +
                     "          }\r\n" +
                     "      }\r\n" +
                     "      Keyword='';\r\n" +
                     "      if(document.getElementById('txtKeyword')!=null)\r\n" +
                     "      {\r\n" +
                     "          Keyword=document.getElementById('txtKeyword').value;\r\n" +
                     "      }\r\n" +
                     "      SurveyCode='';\r\n" +
                     "      if(document.getElementById('txtSurveyCode')!=null)\r\n" +
                     "      {\r\n" +
                     "          SurveyCode=document.getElementById('txtSurveyCode').value;\r\n" +
                     "      }\r\n" +
                     "      Note='';\r\n" +
                     "      if(document.getElementById('txtNote')!=null)\r\n" +
                     "      {\r\n" +
                     "          Note=document.getElementById('txtNote').value;\r\n" +
                     "      }\r\n" +
                     "      ExpiredDate='';\r\n"+
                     "      if(document.getElementById('txtExpiredDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          ExpiredDate=document.getElementById('txtExpiredDate').value;\r\n" +
                     "      }\r\n" +
                     "      AjaxOut = Portal.Cms.Render.UpdateCmsDataRenderBll.ServerSideSave(SiteId, GroupDataId, DataId, Alias, Subject, OtherSubject, Description, Body, Source, Url, Target,  SortIndex, DocType,DocField, DocReleaseFrom,DocSigner,DocStatus,SignDate, ReleaseDate,EffectedDate,EndEffectedDate,ReplaceDoc,ModifyDoc, VoteType, IsNew, Searchable, CategoryIds, HotCategoryIds, NewsTypeId, ImageLevelId, EventId, ExpImageCount, ExpImageLevelId, NewsLevelId, IsFocus, IsShowAuthor, IsAdv, Keyword, SurveyCode, Note, ExpiredDate).value;\r\n" +
                     "      document.getElementById('divCallSaveAddDataProcessing').innerHTML='';\r\n" +
                     "      if(AjaxOut.Error)\r\n" +
                     "      {\r\n" +
                     "          alert(AjaxOut.InfoMessage);\r\n" +
                     "          return;\r\n" +
                     "      }\r\n" +
                     "      if(CloseWindow==1)\r\n"+
                     "      {\r\n"+
                     "          window.open('"+BackUrl+"','_self');\r\n" +
                     "      }\r\n"+
                     "      else\r\n"+
                     "      {\r\n"+
                     "          if(AjaxOut.RetUrl!='')\r\n" +
                     "          {\r\n" +
                     "              window.open(AjaxOut.RetUrl,'_self');\r\n" +
                     "          }\r\n" +
                     "      }\r\n"+
                     "  }\r\n" +
                     "  </script>\r\n";

                //lay xem phu trach chuyen muc nao
                string Categories = OCmsData.Categories;
                string CodeCaption = "Menu hiển thị";               
               
                string ButtonRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                Html +=
                   " <input id=txtCheckedCategory type=hidden value=\"" + OCmsData.CategoryIds + "\">\r\n" +
                   " <input id=txtCheckedHotCategory type=hidden value=\"" + OCmsData.HotCategoryIds + "\">\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:CallSaveCmsData(0);\"><img class=image src=\"" + ButtonRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:CallSaveCmsData(1);\"><img class=image src=\"" + ButtonRoot + "/Save.png\"><div>Ghi lại và đóng</div></li>\r\n" +
                   "            <li onclick=\"" + BackScript + "\"><img class=image src=\"" + ButtonRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "<div class=CssFormSaveUpdate>\r\n" +
                   "  <input type=hidden id=txtDataId value=\"" + CmsDataId + "\">\r\n" +                    
                   "  <div id=divCallSaveAddDataProcessing class=CssProcessing></div>\r\n" +
                   "  <table>\r\n" +
                   "    <tr class=\"CssRowAlias\">\r\n" +
                   "        <td class=c1>" + CodeCaption + ":</td>\r\n" +
                   "        <td class=c2><input id=txtAlias value=\"" + OCmsData.Subject + "\" class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +                   
                   "    <tr " + StyleDisp + ">\r\n" +
                   "        <td class=c1 style=\"vertical-align:top;\">Nội dung:</td>\r\n" +
                   "        <td class=c2>\r\n" +
                   "                <textarea type=hidden id=\"FCKeditorBody\" name=\"FCKeditorBody\">" + OCmsData.Body + "</textarea>\r\n" +
                    "               <script type=\"text/javascript\" src=\"/fckeditor/fckeditor.js\"></script>\r\n" +
                    "               <script type=\"text/javascript\">\r\n" +
                    "                   var oFCKeditor = new FCKeditor('FCKeditorBody');\r\n" +
                    "                   oFCKeditor.BasePath = \"/fckeditor/\";\r\n" +
                    "                   oFCKeditor.Height = \"600\";\r\n" +
                    "                   oFCKeditor.ReplaceTextarea();\r\n" +
                    "               </script>\r\n" +
                   "        </td>\r\n" +
                   "    </tr>\r\n" +                   

                   "    <tr class=\"CssRowUrl\">\r\n" +
                   "        <td class=c1>Url:</td>\r\n" +
                   "        <td class=c2><input id=txtUrl class=textbox type=textbox value=\"" + OCmsData.Url + "\"></td>\r\n" +
                   "    </tr>\r\n" +   
                   "    <tr>\r\n" +
                   "        <td class=c1>Ghi chú:</td>\r\n" +
                   "        <td class=c2><textarea type=textarea class=textarea id=txtNote>" + OCmsData.Note + "</textarea></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr class=\"CssRowUrlType\">\r\n" +
                   "        <td class=c1>Kiểu link:</td>\r\n" +
                   "        <td class=c2><input id=txtTarget class=textbox type=textbox value=\"" + OCmsData.Target + "\"></td>\r\n" +
                   "    </tr>\r\n";               

                //string SelectStatus =
                //        "<select id=drpSelectDocStatus class=drplist>\r\n" +
                //        "   <option value=\"0\">Không sử dụng</option>\r\n" +
                //        "   <option value=\"1\">Sử dụng</option>\r\n" +
                //        "</select>\r\n";
                //if (OCmsData.DocStatus.Equals("1"))
                //{
                //    SelectStatus =
                //    "<select id=drpSelectDocStatus class=drplist>\r\n" +
                //    "   <option value=\"0\">Không sử dụng</option>\r\n" +
                //    "   <option selected value=\"1\">Sử dụng</option>\r\n" +
                //    "</select>\r\n";
                //}               
                Html +=
                   "    <tr class=\"CssRowIndex\">\r\n" +
                   "        <td class=c1>Thứ tự:</td>\r\n" +
                   "        <td class=c2><input id=txtSortIndex class=textbox type=textbox value=\""+OCmsData.SortIndex+"\"></td>\r\n" +
                   "    </tr>\r\n" +
                   //"    <tr class=\"CssRowStatus\">\r\n" +
                   //"        <td class=c1>Trạng thái:</td>\r\n" +
                   //"        <td class=c2>" + SelectStatus + "</td>\r\n" +
                   //"    </tr>\r\n" +
                   
                   "  </table>\r\n" +
                   "</div>\r\n";


                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(
            string SiteId,
            string GroupDataId,
            string DataId,
            string Alias,
            string Subject,
            string OtherSubject,
            string Description,
            string Body,
            string Source,
            string Url,
            string Target,
            string SortIndex,
            string DocType,
            string DocField,
            string DocReleaseFrom,
            string DocSigner,
            string DocStatus,
            string SignDate,
            string ReleaseDate,
            string EffectedDate,
            string EndEffectedDate,
            string ReplaceDoc,
            string ModifyDoc,
            string VoteType,
            int IsNew,
            int AttachedSearchable,
            //danh cho bao

            string CategoryIds,
            string HotCategoryIds,
            string NewsTypeId,
            string ImageLevelId,
            string EventId,
            string ExpImageCount,
            string ExpImageLevelId,
            string NewsLevelId,
            string IsFocus,
            string IsShowAuthor,
            string IsAdv,
            string Keyword,
            string SurveyCode,
            string Note,
            string ExpiredDate)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                //if (string.IsNullOrEmpty(Subject)) throw new Exception("Chưa nhập tiêu đề dữ liệu!");
                if (!string.IsNullOrEmpty(ReleaseDate))
                {
                    if (FunctionUtilities.checkVnDate(ReleaseDate) == false) throw new Exception("Ngày ban hành không hợp lệ!");
                }
                if (!string.IsNullOrEmpty(EffectedDate))
                {
                    if (FunctionUtilities.checkVnDate(EffectedDate) == false) throw new Exception("Ngày hiệu lực không hợp lệ!");
                }
                //DateTime dReleaseDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(ReleaseDate))
                //{
                //    dReleaseDate = FunctionUtilities.VNDateToDate(ReleaseDate);
                //}
                //DateTime dEffectedDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(EffectedDate))
                //{
                //    dEffectedDate = FunctionUtilities.VNDateToDate(EffectedDate);
                //}

                //DateTime dEndEffectedDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(EndEffectedDate))
                //{
                //    dEndEffectedDate = FunctionUtilities.VNDateToDate(EndEffectedDate);
                //}
                //DateTime dSignDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(SignDate))
                //{
                //    dSignDate = FunctionUtilities.VNDateToDate(SignDate);
                //}
               // GroupDataCls OGroupDataCls = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);

                //DateTime dExpiredDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(ExpiredDate))
                //{
                //    dExpiredDate = FunctionUtilities.VNDateToDate(ExpiredDate);
                //}
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DataId);
                OCmsData.DataAlias=Alias;
                OCmsData.Subject=Subject;
                OCmsData.OtherSubject=OtherSubject;
                OCmsData.Description=Description;
                OCmsData.Body=Body;
                OCmsData.Source=Source;
                OCmsData.Url=Url;
                OCmsData.Target=Target;
                try
                {
                    OCmsData.SortIndex = int.Parse(SortIndex);
                }
                catch { }
                OCmsData.frkDocTypeId=DocType;
                OCmsData.frkDocFieldId=DocField;
                OCmsData.frkDocReleaseFromId=DocReleaseFrom;
                OCmsData.frkDocSignerId=DocSigner;
                OCmsData.DocStatus=DocStatus;
                OCmsData.SignDate=SignDate;
                OCmsData.ReleaseDate=ReleaseDate;
                OCmsData.EffectedDate=EffectedDate;
                OCmsData.EndEffectedDate=EndEffectedDate;
                OCmsData.ReplaceDoc=ReplaceDoc;
                OCmsData.ModifyDoc=ModifyDoc;
                OCmsData.VoteType=VoteType;
                OCmsData.IsNew=IsNew;
                OCmsData.AttachedSearchable=AttachedSearchable;
                OCmsData.CategoryIds=CategoryIds;
                OCmsData.HotCategoryIds=HotCategoryIds;
                OCmsData.NewsTypeId=NewsTypeId;
                OCmsData.ImageLevelId=ImageLevelId;
                OCmsData.EventId=EventId;
                try
                {
                    OCmsData.ExpImageCount = int.Parse(ExpImageCount);
                }
                catch { }
                OCmsData.ExpImageLevelId=ExpImageLevelId;
                OCmsData.NewsLevelId=NewsLevelId;
                try
                {
                    OCmsData.IsFocus = int.Parse(IsFocus);
                }
                catch { }
                try
                {
                    OCmsData.IsShowAuthor = int.Parse(IsShowAuthor);
                }
                catch { }
                try
                {
                    OCmsData.IsAdv = int.Parse(IsAdv);
                }
                catch { }
                OCmsData.Keyword=Keyword;
                OCmsData.SurveyCode=SurveyCode;
                OCmsData.Note=Note;
                OCmsData.ExpiredDate = ExpiredDate;

                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).SaveCmsData(null, SiteId, DataId, OCmsData, false);
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }
    }
}
